<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="keHu">
              <a-input v-model="model.keHu" placeholder="请输入客户"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wuLiaoBianMa">
              <j-dict-select-tag
                @input="recallChange()"
                v-model="model.wuLiaoBianMa"
                dictCode="material_code"
                placeholder="请输入物料编码"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="品番" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pinFan">
              <a-input :disabled="isDisabled" v-model="model.pinFan" placeholder="请输入品番"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="供应商代码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="gongYingShangCode"
            >
              <a-input v-model="model.gongYingShangCode" placeholder="请输入供应商代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xingHao">
              <a-input :disabled="isDisabled" v-model="model.xingHao" placeholder="请输入型号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shengChanRiQi">
              <a-input v-model="model.shengChanRiQi" placeholder="请输入生产日期（示例：20230323）"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="班次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="banCi">
              <a-input v-model="model.banCi" placeholder="请输入班次"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单箱数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="danXiangShuLiang">
              <a-input v-model="model.danXiangShuLiang" placeholder="请输入单箱数量"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="总数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zongShu">
              <a-input v-model="model.zongShu" placeholder="请输入总数"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生产批次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shengChanPiCi">
              <a-input v-model="model.shengChanPiCi" placeholder="请输入生产批次"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="唯一序列号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sn">
              <a-input v-model="model.sn" placeholder="请输入唯一序列号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="箱号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xiangHao">
              <a-input v-model="model.xiangHao" placeholder="请输入箱号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="作业员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zuoYeYuan">
              <a-input v-model="model.zuoYeYuan" placeholder="请输入作业员"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="检查员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jianChaYuan">
              <a-input v-model="model.jianChaYuan" placeholder="请输入检查员"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="环保要求" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="huanBaoYaoQiu">
              <a-input v-model="model.huanBaoYaoQiu" placeholder="请输入环保要求"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="二维码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qrCode">
              <a-input v-model="model.qrCode" placeholder="请输入二维码"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ChSpotTicketForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      isDisabled: true,
      model: {
        gongYingShangCode:"SPGYS01089",
        huanBaoYaoQiu:"ROHS符合品"
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        keHu: [{ required: true, message: '请输入客户!' }],
        pinFan: [{ required: true, message: '请输入品番!' }],
        gongYingShangCode: [{ required: true, message: '请输入供应商编码!' }],
        shengChanRiQi: [{ required: true, message: '请输入生产日期!' },{max : 8, message: '请按照示例输入正确的生产日期!'}],
        danXiangShuLiang: [{ required: true, message: '请输入单箱数量!' }],
        zongShu: [{ required: true, message: '请输入总数!' }],
        shengChanPiCi: [{ required: true, message: '请输入生产批次!' }],
        xingHao: [{ required: true, message: '请输入型号!' }],
      },
      url: {
        add: '/ch/chSpotTicket/add',
        edit: '/ch/chSpotTicket/edit',
        queryById: '/ch/chSpotTicket/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    recallChange() {
      if (this.model.wuLiaoBianMa == 'MA020001') {
        this.model.pinFan = 'AN001-40072'
        this.model.xingHao = 'HT-墨水瓶深灰色瓶盖-V1.0'
      }
      if (this.model.wuLiaoBianMa == 'MA020002') {
        this.model.pinFan = 'AN001-40073'
        this.model.xingHao = 'HT-墨水瓶蓝色瓶盖-V1.0'
      }
      if (this.model.wuLiaoBianMa == 'MA020003') {
        this.model.pinFan = 'AN001-40074'
        this.model.xingHao = 'HT-墨水瓶洋红色瓶盖-V1.0'
      }
      if (this.model.wuLiaoBianMa == 'MA020004') {
        this.model.pinFan = 'AN001-40075'
        this.model.xingHao = 'HT-墨水瓶黄色瓶盖-V1.0'
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
